package com.heatup.apt.management.service.impl;

import com.heatup.apt.common.dao.SysRoleMapper;
import com.heatup.apt.common.dao.SysUserMapper;
import com.heatup.apt.common.util.MD5;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.management.service.SysUserService;
import com.heatup.apt.model.management.SysMenu;
import com.heatup.apt.model.management.SysRole;
import com.heatup.apt.model.management.SysUser;
import com.heatup.apt.model.management.SysUserMenu;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {

	@Resource
	private SysUserMapper sysUserMapper;
	@Resource
	private SysRoleMapper sysRoleMapper;
	
	/**
	 * 用户列表
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @param pageIndex 当前页面
	 * @param pageSize 当前页面大小
	 * @param nickName 姓名
	 * @param mobile 手机
	 * @param status 状态
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#queryUserList(int, int, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Map<Object, Object> queryUserList(int pageIndex, int pageSize,String nickName,String mobile,String status) {
		
		Map<Object, Object> resultMap = new HashMap<Object, Object>();
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("pageNo", pageIndex * pageSize);
		params.put("pageSize", (pageIndex + 1) * pageSize);
		params.put("nickName", nickName);
		params.put("mobile", mobile);
		params.put("status", status);
		
		List<SysUser> user_list = sysUserMapper.queryUserList(params);
		resultMap.put("user_list", user_list);
		
		final int user_count = sysUserMapper.queryUserCount(params);
		resultMap.put("user_count", user_count);
		
		return resultMap;
	}

	/**
	 * 获取所有的菜单
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#queryMenuList()
	 */
	@Override
	public List<SysMenu> queryMenuList() {
		List<SysMenu> menu_list = sysUserMapper.queryMenuList();
		return menu_list;
	}
	
	/**
	 * 根据用户ID获取用户的菜单权限
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @param userId
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#queryUserMenuList(java.lang.Integer)
	 */
	@Override
	public List<SysMenu> queryUserMenuList(Integer userId) {
		List<SysMenu> menus = sysUserMapper.queryUserMenuByUserId(userId);
		return menus;
	}


	/**
	 * 查询所有角色list
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#queryAllRole()
	 */
	@Override
	public List<SysRole> queryAllRole() {
		return sysRoleMapper.queryAllRole();
	}


	/**
	 * 添加一个用户
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @param userName 用户名
	 * @param mobile 手机
	 * @param password 密码
	 * @param roleId 角色ID
	 * @param userId 用户ID
	 * @param menusId 菜单权限
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#addUser(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@Override
	public int addUser(String userName, String mobile, String password,Integer roleId,Integer userId,String menusId) {
		int m_cnt = sysUserMapper.queryUserCountByMobile(mobile);
		if (m_cnt > 0) {
			return -1;
		}
		SysUser sysUser = new SysUser();
		sysUser.setMobile(mobile);
		sysUser.setNickName(userName);
		sysUser.setUserName(userName);
		sysUser.setUserPwd(MD5.getMD5String(password));
		sysUser.setRoleId(roleId);
		sysUser.setStatus("1");
		sysUser.setCreatedBy(userId);
		int count = sysUserMapper.insertUser(sysUser);
		if (StringUtil.isNotEmpty(menusId)) {
			String[] menus = menusId.split(",");
			for (String menuId : menus) {
				SysUserMenu sysUserMenu = new SysUserMenu();
				sysUserMenu.setMenuId(Integer.valueOf(menuId));
				sysUserMenu.setUserId(sysUser.getId());
				sysUserMapper.insertUserMenu(sysUserMenu);
			}
		}
		return count;
	}
	

	/**
	 * 修改用户信息
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @param userName 用户名
	 * @param mobile 手机
	 * @param roleId 角色ID
	 * @param userId 用户ID
	 * @param id ID
	 * @param menusId 菜单权限
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#updateUser(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@Override
	public int updateUser(String userName,String mobile,String password,Integer roleId,Integer userId,Integer id,String menusId) {
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("userName", userName);
		params.put("mobile", mobile);
		params.put("roleId", roleId);
		params.put("id", id);
		params.put("updatedBy", userId);
		int count = sysUserMapper.updateUser(params);
		
		sysUserMapper.deleteUserMenuByUserId(id);
		
		if (StringUtil.isNotEmpty(menusId)) {
			String[] menus = menusId.split(",");
			for (String menuId : menus) {
				SysUserMenu sysUserMenu = new SysUserMenu();
				sysUserMenu.setMenuId(Integer.valueOf(menuId));
				sysUserMenu.setUserId(id);
				sysUserMapper.insertUserMenu(sysUserMenu);
			}
		}
		return count;
	}


	/**
	 * 删除用户 by ID
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @param id 用户ID
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#deleteUserById(java.lang.Integer)
	 */
	@Override
	public int deleteUserById(Integer id) {
		sysUserMapper.deleteUserMenuByUserId(id);
		return sysUserMapper.deleteUserById(id);
	}


	/**
	 * 根据ID获取用户
	 * <P>Author : fangxiaowen </P>      
	 * <P>Date : 2016年10月14日 </P>
	 * @param id 用户ID
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#queryUserById(java.lang.Integer)
	 */
	@Override
	public SysUser queryUserById(Integer id) {
		return sysUserMapper.queryUserById(id);
	}


	/**
	 * 更新系统用户
	 *
	 * @param sysUser
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(SysUser sysUser) {

		String menusId = sysUser.getMenusId();
        sysUserMapper.deleteUserMenuByUserId(sysUser.getId());
        //如果密码不为空 可以更新密码
        if (StringUtils.isNotBlank(sysUser.getUserPwd())) {
            sysUser.setUserPwd(MD5.getMD5String(sysUser.getUserPwd()));
        }
        //更新权限
		if (StringUtil.isNotEmpty(menusId)) {
			String[] menus = menusId.split(",");
			for (String menuId : menus) {
				SysUserMenu sysUserMenu = new SysUserMenu();
				sysUserMenu.setMenuId(Integer.valueOf(menuId));
				sysUserMenu.setUserId(sysUser.getId());
				sysUserMapper.insertUserMenu(sysUserMenu);
			}
		}
		return sysUserMapper.updateByPrimaryKeySelective(sysUser);
	}

	/**
	 * 修改密码
	 * <P>Author : fangxiaowen </P>
	 * <P>Date : 2016年10月14日 </P>
	 * @param oldPwd 原密码
	 * @param newPwd 新密码
	 * @param userId 当前用户
	 * @return
	 * @see com.heatup.apt.management.service.SysUserService#updatePassword(java.lang.String, java.lang.String, java.lang.Integer)
	 */
	@Override
	public int updatePassword(String oldPwd, String newPwd,Integer userId) {
		
		if (StringUtil.isEmpty(oldPwd) || StringUtil.isEmpty(newPwd) || StringUtil.isEmpty(userId)) {
			return 0;
		}
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("oldPwd", MD5.getMD5String(oldPwd));
		params.put("newPwd", MD5.getMD5String(newPwd));
		params.put("updatedBy", userId);
		return sysUserMapper.updatePassword(params);
	}
	
	
	
	
	
}
